layui.use(['jquery', 'layer', 'laypage','form'], function() {
	var $ = layui.$;
	var layer = layui.layer;
	var laypage = layui.laypage;
	var form = layui.form;
	var post_collect_json = [];
	var send_info = function(string_uri, json_data, fun_callback) {
		layer.load(2);
		$.ajax({
			type: "post",
			url: string_uri,
			async: true,
			data: json_data,
			dataType: 'json',
			success: function(data) {
				layer.closeAll('loading');
				if (data['code'] == -101) {
					location.href = 'login.html';
					return false;
				} else if (data['code'] == -200) {
					layer.msg(data['msg'], {
						icon: 2,
						anim: 6
					});
					return false;
				} else if (data['code'] == 0 && data['msg'] == '参数错误') {
					layer.msg('参数错误', {
						icon: 2,
						anim: 6
					});
				} else {
					return fun_callback(data);
				}
			},
			error: function(xhr, type, errorThrown) {
				layer.msg('请求失败，访问地址：' + string_uri, {
					icon: 2,
					anim: 6
				});
				layer.closeAll('loading');
			}
		});
	};
	$(document).find('button[lay-filter="submit_collect"]').on('click', function() {
		let post_json = {
			spu: $('input[name="spu"]').val()
		}
		if (post_json.spu == '') {
			layer.msg('请输入要采集的京东商品编号');
			return false;
		}
		var index = layer.load(1, {
			shade: [0.1, '#fff'] //0.1透明度的白色背景
		});
		send_info(collect_base_url, post_json, function(data) {
			layer.close(index);
			if(data.code == 1){
				post_collect_json = data.data;
				$('#caiji_box').html(data.count);
				$('.get-info-flag').show();
			} else {
				layer.msg(data.data);
			}
		})
	})
	$(document).find('button[lay-filter="save_collect"]').on('click', function() {
		post_collect_json.spu = $('input[name="spu"]').val();
		post_collect_json.category_sign1 = $('select[lay-filter="category_collect1"]').val();
		post_collect_json.category_sign2 = $('select[lay-filter="category_collect2"]').val();
		post_collect_json.category_sign3 = $('select[lay-filter="category_collect3"]').val();
		if(post_collect_json.spu == ''){
			layer.msg('输入京东商品编号');
			return false;
		}
		if(!post_collect_json.category_sign1){
			layer.msg('请选择入库分类');
			return false;
		}
		if(!post_collect_json.category_sign2){
			layer.msg('请选择入库分类');
			return false;
		}
		if(!post_collect_json.category_sign3){
			layer.msg('请选择入库分类');
			return false;
		}
		var index = layer.load(1, {
			shade: [0.1, '#fff'] //0.1透明度的白色背景
		});
		send_info(collect_save_url, post_collect_json, function(data) {
			layer.close(index);
			if(data.code == 1){
				layer.msg('商品入库成功', {icon: 1});
				parent.layui.table.reload('jdgoods_list');
				location.reload();
			} else {
				layer.msg('商品入库错误');
			}
		})
	})
   console.log('执行');
	var goods_form_value = {
		category_sign1: '',
		category_sign2: '',
		category_sign3: ''
	};
	
	// 初始化分类
	var form_filter = 'tpl_goodscollect';
	var get_goods_category_list = function(parent_sign, filter) {
		var e = $('form[lay-filter="' + form_filter + '"]').find('#' + filter);
		e.html('');
		e.append('<option value="">加载中...</option>');
		e.prop('disabled', true);
		var id = e.attr('id');
		form.render('select');
		var post_data = {
			parent_sign: parent_sign,
			audit_state: 1
		}
		send_info(category_select_uri, post_data, function(data) {
			e.html('<option value="">请选择分类</option>');
			for (var i in data.data) {
				var temp = data.data[i];
				var op_html = '<option value="' + temp.category_sign + '">' + temp.category_name + '</option>';
				e.append(op_html);
			}
			e.prop('disabled', false);
			form.render('select');
		})
	}
	// 分类联动
	form.on('select(category_collect1)', function(data) {
		var value = data.value;
		var e = data.elem;
		get_goods_category_list(value, 'category_collect2', '');
	})
	form.on('select(category_collect2)', function(data) {
		var value = data.value;
		var e = data.elem;
		get_goods_category_list(value, 'category_collect3', '');
	})
	if (load_data === true) {
		get_goods_category_list(0, 'category_collect1');
	}
})
